package it.polimi.TravelDream;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

@Stateless
public class SearchEngineBean implements SearchEngine {
	
	@PersistenceContext
    private EntityManager em;
        
    @Resource
    private EJBContext context;
    
    private PackageMgrBean PMB = new PackageMgrBean();
    
     @Override
	 public List<FlightDTO> searchFlightDTO(FlightDTO flight)
     {
             List<Flight> myList;
             ArrayList <FlightDTO> myDTOlist = new ArrayList <FlightDTO> ();
             myList = em.createNamedQuery("Flight.searchQuery", Flight.class)
            		 .setParameter("param1", flight.getArrLocation())
            		 .setParameter("param2", flight.getDepLocation())
            		 .setParameter("param3", flight.getDate_Time())
            		 .getResultList();
             for (Flight f : myList)
                 {
                      myDTOlist.add(BaseProductMgrBean.convertFlightToDTO(f));
                 }
             return myDTOlist;
     }
     
     @Override
     public List<HotelDTO> searchHotelDTO(HotelDTO hotel)
     {
    	 List<Hotel> myList;
         ArrayList <HotelDTO> myDTOlist = new ArrayList <HotelDTO> ();
         myList = em.createNamedQuery("Hotel.searchQuery", Hotel.class)
        		 .setParameter("param1", hotel.getCity())
        		 .getResultList();
         for (Hotel h : myList)
             {
                  myDTOlist.add(BaseProductMgrBean.convertHotelToDTO(h));
             }
         return myDTOlist;
     }
     
     @Override
     public List<ExcursionDTO> searchExcursionDTO(ExcursionDTO excursion)
     {
    	 List<Excursion> myList;
         ArrayList <ExcursionDTO> myDTOlist = new ArrayList <ExcursionDTO> ();
         myList = em.createNamedQuery("Excursion.searchQuery", Excursion.class)
        		 .setParameter("param1", excursion.getDate())
        		 .setParameter("param2", excursion.getCity())
        		 .getResultList();
         for (Excursion e : myList)
             {
                  myDTOlist.add(BaseProductMgrBean.convertExcursionToDTO(e));
             }
         return myDTOlist;
     }

     @Override
	 public List<BasePackageDTO> searchBasePackageDTO(BasePackageDTO basepackage)
     {
    	 List<Basepackage> myList;
         ArrayList <BasePackageDTO> myDTOlist = new ArrayList <BasePackageDTO> ();
         myList = em.createNamedQuery("Basepackage.searchQuery", Basepackage.class)
        		 .setParameter("param1", basepackage.getDepLocation())
        		 .setParameter("param2", basepackage.getArrLocation())
        		 .setParameter("param3", basepackage.getDepDate())
        		 .getResultList();
         for (Basepackage b : myList)
             {
                  myDTOlist.add(PackageMgrBean.convertBasePackageToDTO(b));
             }
         return myDTOlist;
    	 
    	 
     }

	@Override
	public SavedPackageDTO searchSavedPackageByID(int packID) {
		SavedPackageDTO tempDTO = new SavedPackageDTO();
		Savedpackage temp ;
		try{temp = em.createNamedQuery("Savedpackage.searchByID", Savedpackage.class)
				.setParameter("param1", packID)
				.getSingleResult();
		}catch(NoResultException e) {return null;};
		tempDTO = PackageMgrBean.convertSavedPackageToDTO(temp);	
		return tempDTO;
	}

	  public User getCurrentUser() {
			return em.createNamedQuery(User.GetCurrent, User.class)
					.setParameter("param", context.getCallerPrincipal().getName())
		            .getSingleResult();
		}
	
	@Override
	public List<SavedPackageDTO> searchSavedPackageByOwner() {
		 List<Savedpackage> myList;
         ArrayList <SavedPackageDTO> myDTOlist = new ArrayList <SavedPackageDTO> ();
         myList = em.createNamedQuery("Savedpackage.searchByOwner", Savedpackage.class)
        		 .setParameter("param1", getCurrentUser().getId())
        		 .getResultList();
         for (Savedpackage s : myList)
             {
                  myDTOlist.add(PackageMgrBean.convertSavedPackageToDTO(s));
             }
         List<SavedPackageDTO> lista = myDTOlist;
         if(lista.size()==0){
        	 lista.add(new SavedPackageDTO());
         }
         return lista;
	}
	
	@Override
	public FlightDTO searchFlightByID(int flightID) {
		Flight flight = new Flight();
		flight = em.find(Flight.class,flightID);
		FlightDTO flightDTO = new FlightDTO();
		flightDTO = BaseProductMgrBean.convertFlightToDTO(flight);
		return flightDTO;
		
	}
	
	@Override
	public HotelDTO searchHotelByID(int hotelID){
		Hotel hotel = new Hotel();
		hotel = em.find(Hotel.class, hotelID);
		HotelDTO hotelDTO = new HotelDTO();
		hotelDTO = BaseProductMgrBean.convertHotelToDTO(hotel);
		return hotelDTO;
	}
	
	@Override
	public List<ExcursionDTO> searchExcursionByBasePackageID(int packageID){
		List<Basepackexlinker> bpel = new ArrayList<Basepackexlinker>();
		List<ExcursionDTO> exclist = new ArrayList<ExcursionDTO>();
		bpel =em.createNamedQuery("Basepackexlinker.findByPack", Basepackexlinker.class)
				.setParameter("param", packageID)
				.getResultList();
		for(Basepackexlinker b : bpel){
			exclist.add(BaseProductMgrBean.convertExcursionToDTO(b.getExcursion()));
		}
		return exclist;
	}

	@Override
	public List<ExcursionDTO> searchExcursionBySavedPackageID(int packageID) {
		List<Packexlinker> spel = new ArrayList<Packexlinker>();
		List<ExcursionDTO> exclist = new ArrayList<ExcursionDTO>();
		spel =em.createNamedQuery("Packexlinker.findByPack",Packexlinker.class)
				.setParameter("param", packageID)
				.getResultList();
		for(Packexlinker b : spel){
			exclist.add(BaseProductMgrBean.convertExcursionToDTO(b.getExcursion()));
		}
		return exclist;
	}

	
				
}
